package com.ke.controller;

import com.ke.pojo.Article;
import com.ke.pojo.ArticleComment;
import com.ke.pojo.AticleInsertPic;
import com.ke.pojo.BaseResp;
import com.ke.service.ArticleService;
import com.ke.utils.UploadUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * @author bochen
 */
@RestController
@RequestMapping("/article")
@CrossOrigin(allowCredentials = "true")
public class ArticleController {

    @Autowired
    ArticleService articleService;

    @RequestMapping("/findArticleByTime")
    public BaseResp findArticleByTime(@RequestBody Map<String, String> map){
        int size= Integer.parseInt(map.get("size"));
        return articleService.findArticleByTime(size);
    }


    @RequestMapping("/findAll")
    public BaseResp findAll(@RequestParam("page") Integer page,@RequestParam("size") Integer size){
        return articleService.findAll(page,size);
    }

    /**
     * 不知用户是否登录执行
     * @param map
     * @return
     */
    @RequestMapping("/findById")
    public BaseResp findById(@RequestBody Map map){
        return articleService.findById((Integer)map.get("id"));

    }

    /**
     * 用户已登陆执行
     * @param map
     * @return
     */
    @RequestMapping("/findByIdUserLogin")
    public BaseResp findByIdUserLogin(@RequestBody Map map,HttpServletRequest request){
        return articleService.findByIdUserLogin((Integer)map.get("id"),request);
    }

    @RequestMapping("/insert")
    public BaseResp insert(@RequestBody Article article, HttpServletRequest request){

        return articleService.updateOrInsert(article,request);
    }

    @RequestMapping("/uploadPic")
    public String uploadPic(MultipartFile file){
        return UploadUtil.upload(file);
    }

    /**
     * 上传文章内部的图片
     * @param aticleInsertPic
     * @return
     */
    @RequestMapping("/upload")
    public Map upload(AticleInsertPic aticleInsertPic){
        return articleService.upload(aticleInsertPic);
    }

    /**
     * 通过文章id增加点赞数
     * @param map
     * @return
     */
    @RequestMapping("/addLikeNumber")
    public BaseResp addLikeNumber(@RequestBody Map map){
        return articleService.addLikeNumber(Integer.valueOf(map.get("id").toString()));
    }

    /**
     * 添加收藏夹
     * @param map
     * @param request
     * @return
     */
    @RequestMapping("/addFactority")
    public BaseResp addFactority(@RequestBody Map map,HttpServletRequest request){
        return articleService.addFactority(Integer.valueOf(map.get("id").toString()),request);
    }

    /**
     * 验证是否已收藏
     * @param map
     * @param request
     * @return
     */
    @RequestMapping("/verifyFactority")
    public BaseResp verifyFactority(@RequestBody Map map,HttpServletRequest request){
        //System.out.println(map);
        return articleService.verifyFactority(Integer.valueOf(map.get("id").toString()),request);
    }

    /**
     * 查询全部收藏夹
     * @param request
     * @return
     */
    @RequestMapping("/findAllFavorites")
    public BaseResp findAllFavorites(HttpServletRequest request){
        return articleService.findAllFavorites(request);
    }

    /**
     * 通过收藏夹得id删除文章
     * @param map
     * @return
     */
    @RequestMapping("/deleteFavorityArticleById")
    public BaseResp deleteFavorityArticleById(@RequestBody Map map){
        return articleService.deleteFavorityArticleById((Integer)map.get("id"));
    }

    /**
     * 添加评论
     * @param articleComment
     * @param request
     * @return
     */
    @RequestMapping("/addArticleCommend")
    public BaseResp addArticleCommend(@RequestBody ArticleComment articleComment,HttpServletRequest request){
        return articleService.addArticleCommend(articleComment,request);
    }

    /**
     * 查询文章得评论
     * @param map
     * @return
     */
    @RequestMapping("/findAllArticleCommend")
    public BaseResp findAllArticleCommend(@RequestBody Map map){
        return articleService.findAllArticleCommend((Integer)map.get("id"));
    }


}
